require "test/unit"
require "./formatter"

class FormatterTests < Test::Unit::TestCase

  # Called before every test method runs. Can be used
  # to set up fixture information.
  def setup
    @formatter = Formatter.new
    @formatter.report_range(Time.local(2010,11,28), Time.local(2010,12,31))
    @formatter.use_subsystem_with_change_count("sub1",30)
    @formatter.use_subsystem_with_change_count("sub2",39)
    @formatter.use_subsystem_with_change_count("sub3",9)
    @output_lines = @formatter.output
  end

  # Called after every test method runs. Can be used to tear
  # down fixture information.

  def teardown
    # Do nothing
  end

  # Fake test
#  def test_fail
#
#    # To change this template use File | Settings | File Templates.
#    fail("Not implemented")
#  end

  # new tests for new methods

  def test_header_comes_before_subsystem_lines
    assert_match(/Changes between/, @output_lines[0])
  end

  def test_both_lines_are_present_in_descending_change_count_order
    assert_match(/sub2.*39/, @output_lines[1])
    assert_match(/sub1.*30/, @output_lines[2])
  end

  def test_nothing_else_is_present
    assert_equal(4, @output_lines.size)
  end

  # old tests
  
  def test_header_format
    head = @formatter.header
    assert_equal("Changes between 2010-11-28 and 2010-12-31:", head)
  end

  def test_image_for_divides_by_five
    assert_equal("****", @formatter.image_for(20))
  end

  def test_image_for_rounds_up_and_down
    assert_equal('****', @formatter.image_for(18))
    assert_equal('***', @formatter.image_for(17))
  end

  def test_image_for_zero_is_a_dash
    assert_equal('-', @formatter.image_for(0))
  end

  def test_image_for_rounds_up_small_number
    assert_equal('*', @formatter.image_for(1))
    assert_equal('*', @formatter.image_for(2))
    assert_equal('*', @formatter.image_for(5))
    assert_equal('*', @formatter.image_for(7))
    assert_equal('**', @formatter.image_for(8))
  end

  def test_normal_subsystem_line_format
    assert_equal('audit          (45 changes)   *********',
                 @formatter.subsystem_line("audit", 45))
  end

  def test_subsystem_line_has_special_format_for_zero_changes
    assert_equal('data           -              -',
                 @formatter.subsystem_line("data", 0))
  end

  def test_churn_line_to_int_extracts_parenthesized_change_count
    assert_equal(51,
                 @formatter.churn_line_to_int(" (51 changes)   **********"))
    assert_equal(2,
                 @formatter.churn_line_to_int(" (2 changes)   **********"))
  end

  def test_order_by_descending_change_count
    sys1 = "sub1           (30 changes)   ******"
    sys2 = "sub2           (39 changes)   ********"
    sys3 = "sub3           (9 changes)    **"
    assert_equal([sys2, sys1, sys3], @formatter.order_by_descending_change_count)
  end

  def test_interesting_lines_contain_at_least_one_asterisk

    boring_line =      "test           -    -"
    interesting_line = "keting.docx    (4 changes)    *"
    big_line =         "jiazhuang.xlsx (51 changes)   **********"

    original = [interesting_line, boring_line, big_line]
    expected = [interesting_line, big_line]

    assert_equal(expected, @formatter.interesting(original))
  end

  def test_interesting_lines_subsystem_can_have_asterisk_at_end
    boring_line = @formatter.subsystem_line('inventory*', 0)
    interesting_line = @formatter.subsystem_line('ui*', 50)

    original = [interesting_line, boring_line]
    expected = [interesting_line]

    assert_equal(expected, @formatter.interesting(original))
  end
end